﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;


public partial class PersonalManager_Department : System.Web.UI.Page
{

    List<Branch> branchList = new BranchManager().GetAllBranch();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            BindData();
        }
    }
    private void BindData()
    {

        foreach (Branch item in branchList)
        {

            TreeNode tn = new TreeNode("&nbsp;&nbsp;" + item.BranchShortName);
            tn.Value = item.BranchId.ToString();
            tn.ImageUrl = "~/imges/dept_tree.jpg";
            this.tvDeparment.Nodes.Add(tn);
            InitTreeByList(tn);

        }
    }
    private void InitTreeByList(TreeNode TN)
    {
        List<Department> deptList = new DeptManager().GetAllDepartment();
        foreach (Department item in deptList)
        {
            if (item.ParentId.ToString() == TN.Value)
            {
                TreeNode tn = new TreeNode();
                tn.Text = "&nbsp;&nbsp;" + item.DeptName;
                tn.Value = item.DeptId.ToString();
                //tn.NavigateUrl = item.NodeURL;
                tn.ImageUrl = "~/imges/dept_tree_block.jpg";
                TN.ChildNodes.Add(tn);
                InitTreeByList(tn);
            }


        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

        Department dept = new Department();
        dept.DeptName = this.txtName.Text.Trim();
        dept.Description = this.txtDesc.Text.Trim();
        dept.DeptCode = this.txtCode.Text.Trim();
        dept.ParentId = Convert.ToInt32(tvDeparment.SelectedNode.Value);
        if (this.rbOK.Checked == true)
        {
            dept.Status = 1;

        }
        if (this.rbNO.Checked == true)
        {
            dept.Status = 1;
        }
        int nodeId = Convert.ToInt32(tvDeparment.SelectedNode.Value);
        bool result = false;
        foreach (Branch item in branchList)
        {
            if (item.BranchId == nodeId)
            {
                result = true;
            }
        }
        if (result == true)
        {
            Branch bb = new Branch();
            bb.BranchId = nodeId;
            bb.BranchShortName = tvDeparment.SelectedNode.Text;
            dept.Branch = bb;
        }
        else
        {
            int parId = Convert.ToInt32(tvDeparment.SelectedNode.Parent.Value);
            foreach (Branch item in branchList)
            {
                if (item.BranchId == parId)
                {
                    result = true;
                }
            }
            if (result == true)
            {
                Branch bb = new Branch();
                bb.BranchId = parId;
                bb.BranchShortName = tvDeparment.SelectedNode.Parent.Text;
                dept.Branch = bb;
            }
            else
            {
                int parparId = Convert.ToInt32(tvDeparment.SelectedNode.Parent.Parent.Value);
                foreach (Branch item in branchList)
                {
                    if (item.BranchId == parparId)
                    {
                        result = true;
                    }
                }
                if (result == true)
                {
                    Branch bb = new Branch();
                    bb.BranchId = parparId;
                    bb.BranchShortName = tvDeparment.SelectedNode.Parent.Parent.Text;
                    dept.Branch = bb;
                }
                else
                {
                    int parparparId = Convert.ToInt32(tvDeparment.SelectedNode.Parent.Parent.Parent.Value);
                    foreach (Branch item in branchList)
                    {
                        if (item.BranchId == parparparId)
                        {
                            result = true;
                        }
                    }
                    if (result == true)
                    {
                        Branch bb = new Branch();
                        bb.BranchId = parparparId;
                        bb.BranchShortName = tvDeparment.SelectedNode.Parent.Parent.Parent.Text;
                        dept.Branch = bb;
                    }

                }
            }
        }
        new DeptManager().AddDept(dept);
        tvDeparment.Nodes.Clear();
        BindData();
    }

}